package com.myeclipse.search;

import java.util.ArrayList;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.search.ui.ISearchResultListener;
import org.eclipse.search.ui.SearchResultEvent;

public class SearchResult implements ISearchResult{

	private SearchQuery query;
	private ListenerList listeners=new ListenerList();

	private ArrayList<Code> result=new ArrayList<Code>();
//	private ArrayList<ISearchResultListener> searchResultsListeners=new ArrayList<ISearchResultListener>();
	@Override
	public void addListener(ISearchResultListener l) {
		// TODO Auto-generated method stub
		listeners.add(l);
//		searchResultsListeners.add(l);

	}

	public SearchResult(SearchQuery query){
		this.query=query;
	}
	
	@Override
	public void removeListener(ISearchResultListener l) {
		// TODO Auto-generated method stub
		listeners.remove(l);
	//	searchResultsListeners.remove(l);
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "label";
	}

	@Override
	public String getTooltip() {
		// TODO Auto-generated method stub
		return "tooltip";
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	/*Return the Query*/
	@Override
	public ISearchQuery getQuery() {
		// TODO Auto-generated method stub
		return query;
	}

	/*Return the SearchResult*/
	public ArrayList<Code> getResult()
	{
		return result;
	}
	
	/*Add matched Code to the SearchResult.*/
	public void add(Code match){
		result.add(match);
		Object[] oListeners =  listeners.getListeners();
 		SearchResultEvent sre = new SearchResultEvent(this) {
			private static final long serialVersionUID = -4033648093546820133L;
		};
		for (int i = 0; i < oListeners.length; i++) {
			((ISearchResultListener) oListeners[i]).searchResultChanged(sre);
		}
	}
	public int getTotal(){
		return query.getTotal();
	}

}
